package com.qumitech.visualnovel.service.updatecfg.impl;

import com.qumitech.visualnovel.component.util.DateUtil;
import com.qumitech.visualnovel.component.util.TimeUtils;
import com.qumitech.visualnovel.datasource.app.BookUpdateCfgDOMapper;
import com.qumitech.visualnovel.datasource.editor.BookDAOMapper;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.app.BookUpdateCfgDO;
import com.qumitech.visualnovel.model.dao.app.BookUpdateCfgDOExample;
import com.qumitech.visualnovel.model.dao.editor.BookDAO;
import com.qumitech.visualnovel.model.dto.BookNumberDTO;
import com.qumitech.visualnovel.model.dto.updatecfg.BookOptionDTO;
import com.qumitech.visualnovel.model.dto.updatecfg.BookUpdateCfgDTO;
import com.qumitech.visualnovel.model.dto.updatecfg.BookUpdateCfgQueryDTO;
import com.qumitech.visualnovel.pubdef.constant.BookPublishStateEnum;
import com.qumitech.visualnovel.service.updatecfg.BookUpdateCfgService;
import com.qumitech.visualnovel.service.updatecfg.constant.BookUpdateCfgTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class BookUpdateCfgServiceImpl implements BookUpdateCfgService {
    @Autowired
    private BookUpdateCfgDOMapper cfgDOMapper;
    @Autowired
    private BookDAOMapper bookDAOMapper;


    @Override
    public BaseResponse listCfg(BookUpdateCfgQueryDTO cfgQueryDTO) {
        BookUpdateCfgDOExample example = new BookUpdateCfgDOExample();
        BookUpdateCfgDOExample.Criteria criteria = example.createCriteria().andDeleteFlagEqualTo(false);
        if (cfgQueryDTO.getStatus() != null) {
            criteria.andStatusEqualTo(cfgQueryDTO.getStatus());
        }
        if (cfgQueryDTO.getCfgType() != null) {
            criteria.andCfgTypeEqualTo(cfgQueryDTO.getCfgType());
        }
        if (cfgQueryDTO.getOrder() != null) {
            criteria.andOrderIndexEqualTo(cfgQueryDTO.getOrder());
        }
        if (StringUtils.isNotEmpty(cfgQueryDTO.getTitle())) {
            criteria.andBookTitleEqualTo(cfgQueryDTO.getTitle());
        }
        if (StringUtils.isNotEmpty(cfgQueryDTO.getStartTime())) {
            criteria.andStartEffectTimeGreaterThan(TimeUtils.parse(cfgQueryDTO.getStartTime(), TimeUtils.DATE_FORMAT_DATE_S));
        }
        if (StringUtils.isNotEmpty(cfgQueryDTO.getEndTime())) {
            criteria.andStartEffectTimeLessThan(TimeUtils.parse(cfgQueryDTO.getEndTime(), TimeUtils.DATE_FORMAT_DATE_S));
        }
        List<BookUpdateCfgDTO> cfgDTOList = cfgDOMapper.selectByExample(example).stream().map(BookUpdateCfgDTO::new).collect(Collectors.toList());
        return new BaseResponse(cfgDTOList);
    }

    @Override
    public BaseResponse addCfg(BookUpdateCfgDTO cfgDTO) {
        BookUpdateCfgDO cfgDO = cfgDTO.convertDO();
        cfgDO.setBookTitle(bookDAOMapper.selectByPrimaryKey(cfgDO.getBookId().longValue()).getTitle());
        cfgDOMapper.insertSelective(cfgDO);
        return new BaseResponse();
    }

    @Override
    public BaseResponse updateCfg(BookUpdateCfgDTO cfgDTO) {
        cfgDOMapper.updateByPrimaryKeySelective(cfgDTO.convertDO());
        return new BaseResponse();
    }

    @Override
    public BaseResponse delCfg(Long id) {
        BookUpdateCfgDO cfgDO = new BookUpdateCfgDO();
        cfgDO.setId(id.intValue());
        cfgDO.setDeleteFlag(true);
        cfgDOMapper.updateByPrimaryKeySelective(cfgDO);
        return new BaseResponse();
    }

    @Override
    public BaseResponse listOptions(Integer cfgType) {
        String publishState = "";
        if (cfgType.equals(BookUpdateCfgTypeEnum.UPDATE.getType())) {
            publishState = BookPublishStateEnum.ONGOING.getStateStr();
        }
        if (cfgType.equals(BookUpdateCfgTypeEnum.UPCOMING.getType())) {
            publishState = BookPublishStateEnum.UPCOMING.getStateStr();
        }
        List<BookOptionDTO> optionDTOS = bookDAOMapper.queryByPublishState(publishState).stream()
                .map(BookOptionDTO::new).collect(Collectors.toList());
        return new BaseResponse(optionDTOS);
    }
}
